import $ from '../jquery-3.4.1.min.js';
import ajax from '../utils/ajax';

const searchData = { value: '' };



// 点击添加广告位里面的新增，发送ajax
$('#advertising .addAdvertising .add').click(function () {
    const advertisingNumber = $('#advertising .advertisingNumber').val();
    const advertisingName = $('#advertising .advertisingName').val();
    const advertisingState = $('#advertising .advertisingState').val();
    const community_id = $('#advertising .communityName').val();
    const advertisingLocation = $('#advertising .advertisingLocation').val();
    const advertisingSize = $('#advertising .advertisingSize').val();
    const chargeStandard = $('#advertising .chargeStandard').val();
    const advertisingPrice = $('#advertising .advertisingPrice').val();
    ajax({
        url: '/advertising',
        type: 'POST',
        data: {
            advertisingNumber
            , advertisingName
            , advertisingState
            , community_id
            , advertisingLocation
            , advertisingSize
            , chargeStandard
            , advertisingPrice
        },
        success(res) {
            if (res.code == 200) {
                alert('广告位新增成功');
                getAdvertising();
            }
        }
    })
})



// 获取小区，渲染到添加广告位的小区select框里面
//添加广告位模态框显示和隐藏
$('#advertising .addAdvertising').hide()
$('#advertising .advertising-space-nav span').click(function () {
    const spanText = $(this).text();
    if (spanText == '添加') {
        $('#advertising .addAdvertising').show()
        ajax({
            url: '/community',
            type: 'GET',
            data: {
                value: searchData.value
            },
            success(res) {
                defaultSelect(res.result);

            }
        })
    }
})

function defaultSelect(data) {
    const html = data.map(item => (
        `
        <option value="${item._id}">${item.communityName}</option>
        `
    )).join('');
    $('#advertising .communityName').html(html);

}

// 隐藏模态框
$('#advertising .addAdvertising button').click(function () {
    $('#advertising .addAdvertising').hide()
    getAdvertising();
})

// 渲染所有的广告位
getAdvertising();
function getAdvertising() {
    ajax({
        url:'/advertising',
        type:'GET',
        data:{value:searchData.value},
        success(res){
            // console.log(res);
            defaultAdvertising(res.result)
        }
    })
}

function defaultAdvertising(data) {
    const html = data.map(items => (
        `<tr>
            <td>${items.advertisingNumber}</td>
            <td>${items.advertisingName}</td>
            <td>${items.advertisingState}</td>
            <td>${items.community_id.communityName}</td>
            <td>${items.advertisingLocation}</td>
            <td>${items.advertisingSize}m²</td>
            <td>${items.chargeStandard}</td>
            <td>${items.advertisingPrice}</td>
            <td>
                <span>详情</span>
                <span>添加收费标准</span>
                <span>编辑</span>
                <span data-id="${items._id}">删除</span>
            </td>
    </tr>`
    )).join('');

    $('#advertising tbody').html(html);
}

// 查询
$('#advertising .sousuo').click(function () {
    searchData.value = $('#advertising .number').val();
    getAdvertising();
})

// 删除
$('#advertising tbody').on('click', 'span', function () {
    const spanText = $(this).text();
    if (spanText == '删除') {
        const _id = $(this).attr('data-id')
        ajax({
            url: '/advertising',
            type: 'DELETE',
            data: { _id },
            success(res) {
                if (res.code == 200) {
                    alert('删除成功')
                    getAdvertising()
                }
            }
        })
    }
})





